package com.laikan.reader.bookfriend.adapter.impl;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.laikan.reader.R;
import com.laikan.reader.bookfriend.holder.AttentionHolder;
import com.laikan.reader.bookfriend.utils.ImageUtils;
import com.laikan.reader.core.base.BaseHolder;

import java.util.List;

public class AttentionAdapter extends BaseAdapter {

    private List mDatas;

    private MyFooterHolder mFooterHolder;
    @Override
    public int getCount() {
        return null == mDatas ? 0 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas == null ? null:mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseHolder mHolder;
        if (convertView == null) {
            mHolder = getHolder();
        } else {
            mHolder = (BaseHolder) convertView.getTag();
        }

        Object item = getItem(position);
        mHolder.setData(item);
        return mHolder.getRootView();
    }


    public void addData(List list) {
        if (null != mDatas && mDatas.size() > 0) {
            mDatas.addAll(list);
        } else {
            setData(list);
        }
    }

    public void setData(List mDatas) {
        this.mDatas = mDatas;
    }

    public List getData() {
        return mDatas;
    }

    public void delete(int position) {
        if (mDatas != null) {
            mDatas.remove(position);
        }
    }

    public void clear() {
        if (null != mDatas) {
            mDatas.clear();
        }
    }

    public BaseHolder getHolder() {
        return new AttentionHolder();
    }

    public void setHasMoreState(boolean hasMore){
        getMoreHolder().setData(hasMore);
    }


    public MyFooterHolder getMoreHolder() {
        if (mFooterHolder == null){
            mFooterHolder = new MyFooterHolder();
        }
        return mFooterHolder;
    }

    public class MyFooterHolder extends BaseHolder{

        @Override
        public View initView() {
            View view = View.inflate(ImageUtils.getContext(),R.layout.item_comment_foot_view,null);
            view.setVisibility(View.GONE);
            return view;
        }

        @Override
        public void refreshView() {
            boolean hasMore = (boolean) getData();
            view.setVisibility(hasMore?View.INVISIBLE:View.VISIBLE);
        }
    }
}

